﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core.Util;
using ZHDJ.Data;
using ZHDJ.Data.Models.Forum;
using ZHDJ.WebPage;

namespace ZHDJ.Web.Portal.Forum
{
    public partial class BBSReplyDetails : PortalPage
    {
    
        #region 属性
        protected string TypeID
        {
            get
            {
                if (!string.IsNullOrEmpty(ForumID))
                {
                    var forum=DB.PartyForums.Find(ForumID);
                    if (forum != null)
                    {
                        return forum.TypeID;
                    }
                   
                    return string.Empty;
                }
                return string.Empty;
            }
        }

        public string PartyForumTypeName
        {
            get
            {
                var type = DB.PartyForumTypes.Find(TypeID);
                if (type != null)
                {
                    return type.Title;
                }
                return "";
            }

        }

        /// <summary>
        /// 版块名称
        /// </summary>
        public string TypeName
        {
            get
            {
                if (string.IsNullOrEmpty(this.TypeID))
                {
                    return "版块信息";
                }
                var type= DB.PartyForumTypes.Find(this.TypeID);
                if (type!=null)
                {
                    return type.Title;
                }
                return "版块信息";
            }
        }

        /// <summary>
        /// 当前页数
        /// </summary>
        protected int PageIndex
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["PageIndex"]))

                    try
                    {
                        return Math.Abs(Convert.ToInt32(Request.QueryString["PageIndex"]));
                    }
                    catch (Exception)
                    {

                        return 0;
                    }
                else
                    return 0;
            }
        }

        protected int PageSize
        {
            get
            {
                try
                {
                    return Math.Abs(Convert.ToInt32(this.hdPageSize.Value)) ;
                }
                catch (Exception)
                {
                    return 20;
                }
            }
        }

        protected int TotalCount
        {
            get
            {
                return DB.PartyForumReplys.Count(a => a.ForumID==this.ForumID && string.IsNullOrEmpty(a.ReplyBy));
            }
        }

        private DataSet _DataSource;
        protected DataTable DataSource
        {
            get
            {
                return _DataSource.Tables[0];
            }
        }

        
        /// <summary>
        /// 帖ID
        /// </summary>
        public string ForumID
        {
            get
            {
                var id = StringHelper.RemoveHTML(Request.QueryString["ForumID"]);
                id = StringHelper.ReplaceChar(id);
                return id;
            }
        }

        public int PageCount
        {
            get { return TotalCount > PageSize ? ((TotalCount % PageSize) > 0 ? (TotalCount / PageSize) + 1 : TotalCount / PageSize) : 1; }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            this.CheckLogin(true);
           
            //状态为0的党员有权限访问
            
            if (CurrentUser.runSate == "0")
            {
                this.divFatie.Visible = true;
                this.divFatie2.Visible = true;
                this.div_post.Visible = true;//回复
            }
            else
            {
                this.divFatie.Visible = false;
                this.divFatie2.Visible = false;
                this.div_post.Visible = false;//回复
            }
           
            if (!IsPostBack)
            {
               
                    _DataSource = PartyForum.GetForumInfoByID(this.ForumID, PageIndex, PageSize);
                this.hdTotalCount.Value = _DataSource.Tables[1].Rows[0][0].ToString();

                this.hdForumID.Value = this.ForumID;
                hdPageIndex.Value = PageIndex.ToString();
                BindData();
                AddBrowseData();
            }

            iContent2.Attributes["src"] = "BBSReplyCK.aspx?ForumID=" + ForumID + "&PageIndex=" + PageIndex;
        }

        #region 方法
        /// <summary>
        /// 是否是版块管理员
        /// </summary>
        /// <returns></returns>
        public bool IsSystemAdministrator()
        {
            bool isSystemAdministrator = false;
            if (CurrentUser.LoginID.ToLower() == "admin")
                isSystemAdministrator = true;
            else if (PartyForumTypeAdmin.IsForumTypeAdmin(this.TypeID, CurrentUser.LoginID))
                isSystemAdministrator = true;
            else
            {
                var type = DB.PartyForumTypes.Find(this.TypeID);
                if (type != null)
                {
                    if (PartyForumTypeAdmin.IsForumTypeAdmin(type.ParentID,
                        CurrentUser.LoginID))
                        isSystemAdministrator = true;
                }
            }
            return isSystemAdministrator;
        }

       

        public static bool IsSystemAdministrator(string typeID )
        {
            bool isSystemAdministrator = false;
            if (CurrentUserManager.CurrentUser.LoginID.ToLower() == "admin")
                isSystemAdministrator = true;
            else if (PartyForumTypeAdmin.IsForumTypeAdmin(typeID, CurrentUserManager.CurrentUser.LoginID))
                isSystemAdministrator = true;
            else
            {
                var forum = DB.PartyForumTypes.Find(typeID);
                if (forum != null)
                {
                    if (PartyForumTypeAdmin.IsForumTypeAdmin(forum.ParentID, CurrentUserManager.CurrentUser.LoginID))
                        isSystemAdministrator = true;
                }
            }
            return isSystemAdministrator;
        }

        /// <summary>
        /// 验证当前用户是否发帖人
        /// </summary>
        /// <returns></returns>
        public bool IsForumMaster()
        {
            bool isForumMaster = false;
            var forum=DB.PartyForums.Find(this.ForumID);
            if (forum != null && forum.Author == CurrentUser.LoginID)
                isForumMaster = true;
            return isForumMaster;
        }
        

        /// <summary>
        /// 验证当前用户是否回复人
        /// </summary>
        /// <returns></returns>
        public bool IsReplyMaster(string replyID)
        {

            return
                DB.PartyForumReplys.Any(a => a.ID == replyID && a.ReplyBy == CurrentUserManager.CurrentUser.LoginID);
        }

        protected void BindData()
        {
            if (!string.IsNullOrEmpty(this.ForumID))
            {
                var forum = DB.PartyForums.Find(this.ForumID);
                if (forum != null)
                {
                    forum.VisitTimes += 1;
                    forum.TodayVisitTimes += 1;
                    DB.SaveChanges();
                }
                lblTitle2.Text = string.Format("<a href=\"BBSList.aspx?TypeID={0}\">{1}</a>", forum.TypeID, PartyForumTypeName);
            }
        }

      

        public void AddBrowseData()
        {

            DB.PartyForumBrowses.Add(new PartyForumBrowse()
            {
                ID=StringHelper.NewID, BrowseTime = DateTime.Now, BrowseUserID=CurrentUser.LoginID, ForumID=this.ForumID
            });
            DB.SaveChanges();
        }

      
        #endregion

        #region 事件

        public string GetBBSIamge(string bbsIamge)
        {
            if (string.IsNullOrEmpty(bbsIamge))
            {
                return "../../Resource/Themes/Default/Images/Widget/BBS_noavatar_medium.gif";
            }
            else
            {
                return UpFileBusiness.GetFileUrl(bbsIamge);
            }
        }
        #endregion

        #region WebMethod
        [WebMethod]
        public static bool DelForum(string id, bool isReply)
        {
            var forum = DB.PartyForums.Find(id);
            var reply = DB.PartyForumReplys.Find(id);
            return PartyForum.DeleteForum(id, isReply,CurrentUserManager.CurrentUser.LoginID);
        }

        [WebMethod]
        public static bool SetJinHuaForum(string id, bool isJinHua)
        {
            return PartyForum.SetJinHuaForum(id, isJinHua, CurrentUserManager.CurrentUser.LoginID);
        }

        [WebMethod]
        public static bool SetTopForum(string id, bool isTop)
        {
            return PartyForum.SetTopForum(id, isTop,CurrentUserManager.CurrentUser.LoginID);
        }

        [WebMethod]
        public static bool SetJuJiaoForum(string id, string isFocus)
        {
            return PartyForum.SetJuJiaoForum(id, isFocus,  CurrentUserManager.CurrentUser.LoginID);
        }
        #endregion


    }
}